﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using RentAWorker.Business.Interfaces;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.Collections.ObjectModel;
using RentAWorker.Desktop.Entities;
using System.ComponentModel;
using RentAWorker.Management.Infrastructure;
using RentAWorker.Management.Infrastructure.CommunicationServices;
using System.Windows;

namespace CustomerManagement.WorkerList
{
    public class CustomerListPresentationModel : ICustomerListPresentationModel, INotifyPropertyChanged,
                                                IHeaderInfoProvider<string>, IClosingAware
    {
        private IUnityContainer Container { get; set; }

        // Services
        private IManageCustomerService CustomerService { get; set; }
        private IRentManagementCommService RentManagementModuleCommService { get; set; }

        // Bindable commands
        public ICommand SearchCommand { get; set; }
        public ICommand ShowRentalsCommand { get; set; }

        // Bindable data
        public ObservableCollection<Customer> CustomerList { get; set; }
        public bool IsRentManagementModulePresent { get; set; }

        public CustomerListPresentationModel(IUnityContainer container, ICustomerListView view,
                                            IManageCustomerService service)
        {
            this.Container = container;
            this.View = view;
            this.View.Model = this;

            this.CustomerService = service;

            if (this.Container.CanResolve<IRentManagementCommService>())
            {
                this.RentManagementModuleCommService = this.Container.Resolve<IRentManagementCommService>();
            }
            this.IsRentManagementModulePresent = (this.RentManagementModuleCommService != null);

            this.CustomerList = new ObservableCollection<Customer>();
            this.SearchCommand = new DelegateCommand<string>(SearchCustomer);
            this.ShowRentalsCommand = new DelegateCommand<Customer>(ShowRentals);

            this.HeaderInfo = "Customer List";
        }

        private void ShowRentals(Customer target)
        {
            if (target != null)
            {
                int customerId = target.CustomerID;

                this.RentManagementModuleCommService.ShowRentsForCustomer(customerId);
            }
        }

        private void SearchCustomer(string searchString)
        {
            this.CustomerList.Clear();

            foreach (var item in
                this.CustomerService.GetCustomers(searchString))
            {
                this.CustomerList.Add(item);
            }
        }

        #region IWorkerListPresentationModel Members

        public ICustomerListView View { get; set; }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region IHeaderInfoProvider<string> Members

        string _header = default(string);

        public string HeaderInfo
        {
            get
            {
                return _header;
            }
            set
            {
                _header = value;
                OnPropertyChanged("Header");
            }
        }

        #endregion

        #region IClosingAware Members

        public bool RequestClose()
        {
            return this.CanClose;
        }

        public bool CanClose
        {
            get { return true; }
        }

        #endregion
    }
}
